<?php

class Province{

	private $color, $name;
	private $defending_unit;
	private $defending_support_unit = array();
	private $attacking_unit = array();
	private $attacking_support_unit = array();


	protected function __construct(){
		
	}
	
	public function setColor($color){
		$this->color = $color;
	}

	public function getColor(){
		return $this->color;
	}

	protected function setName($name){
		$this->name = $name;
	}

	public function getName(){
		return $this->name;
	}

	public function setDefending_unit(& $defending_unit){
		echo '<b>' . $this->name . "</b> now contains unit <b>" . $defending_unit->getUnitId() . '</b></br>'; //test-code
		$this->defending_unit = $defending_unit;
		$defending_unit->setStartPos($this);
	}

	protected function getDefending_unit(){
		return $this->defending_unit;
	}

	public function setDefending_support_unit(& $support_def_unit){
		echo '<b>' . $this->getName() . '</b> has <b>' . $support_def_unit->getUnitId() . '</b> as a new defending supporting unit</br>'; //test-code
		$this->defending_support_unit[] = $support_def_unit;
	}

	protected function getDefending_support_unit(){
		return $this->defending_support_unit[0]->getUnitId();
	}

	public function setAttacking_unit(& $attack_unit){
		echo '<b>' . $this->getName() . '</b> has <b>' . $attack_unit->getUnitId() . '</b> as a new attacking unit</br>'; //test-code
		$this->attacking_unit[] = $attack_unit;
	}
	protected function getAttacking_unit(){
		return $this->attacking_unit[0];
	}

	public function setAttacking_support_unit(& $support_atk_unit){
		echo '<b>' . $this->getName() . '</b> has <b>' . $support_atk_unit->getUnitId() . '</b> as a new attacking supporting unit</br>'; //test-code
		$this->attacking_support_unit[] = $support_atk_unit;
	}
	
	protected function getAttacking_support_unit(){
		return $this->attacking_support_unit[0]->getUnitId();
	}

	public function resolve(){
		if(count($this->attacking_unit)>0){
			echo '<br><i>Starting resolve-function in </i><b>' . $this->getName() . '</b><br>'; //test-code
			if($this->defending_unit)
				echo 'Defender: <b>' . $this->getDefending_unit()->getUnitId() . '</b><br>'; //test-code
			echo 'Attacker: <b>' . $this->getAttacking_unit()->getUnitId() . '</b><br>'; //test-code
			if($this->defending_support_unit)
				echo 'Support defender: <b>' . $this->getDefending_support_unit() . '</b><br>'; //test-code
			if($this->attacking_support_unit)
				echo 'Support attacker: <b>' . $this->getAttacking_support_unit() . '</b><br>'; //test-code
			echo '<i>Validating data</i><br>'; //test-code


			if(count($this->attacking_unit)==1){
				if((count($this->defending_unit) + count($this->defending_support_unit))>=(count($this->attacking_unit) + count($this->attacking_support_unit))){
					echo '<b>Defenders won!</b><br><br>'; //test-code
					//print_r($this);
					//reset attackers and support
					//unset($this->attacking_unit);
					//unset($this->attacking_support_unit);
					$this->attacking_unit = array();
					$this->attacking_support_unit = array();
					$this->defending_support_unit = array();
					//print_r($this);
				}


				else{
					echo '<b>Attackers won!</b><br>'; //test-code
					if($this->defending_unit != null)
						$this->getDefending_unit()->setDispatched();
					$this->setDefending_unit($this->attacking_unit[0]);
				}
			}else{
				echo 'To many attackers, doing nothing';
			}


		}else{
			echo 'No attackers, doing nothing<br>'; //test-code
		}
	}
	
}

?>
